<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
     <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
     <link rel="stylesheet" href="/css/common.css" media="all">
    <script src="/js/layui/layui.all.js"></script>
    <script src="/admin/js/token.js"></script>
</head>
<style>
    .formbox {
        height: 1px;
    }

    body {
        background-color: white;
    }
</style>
<body>
<div class="layui-row">
    <div class="layui-col-md3  formbox ">
    </div>


    <div class="layui-fluid layui-col-md6 layui-col-sm12 ">
        <div class="layui-card">
            <div class="layui-card-body" style="padding: 15px;" class="layui-row">
                <form class="layui-form" action="" lay-filter="component-form-group">


                    <div class="layui-form-item">
                        <label class="layui-form-label">apk上传：</label>
                        <div class="layui-input-block">
                            <a class="layui-btn" id="upFile"><i class="layui-icon layui-icon-upload"></i>预览</a>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">应用名称：</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" id="apkName" lay-verify="required" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">应用包名：</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" id="apkPackage" class="layui-input" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">图片：</label>
                        <div class="layui-input-block">
                            <img id="apkPictureUrl" src="/images/jiahao.jpg" width="50px" height="50px"><br>
                            <span style="color:red">*点击图片框上传图片</span>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">是否公开：</label>
                        <div class="layui-input-block">
                            <select name="public" id="apkIsItPublic">
                                <option value="不公开">否</option>
                                <option value="公开">是</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">审核状态：</label>
                        <div class="layui-input-block">
                            <select name="shenhe" id="apkShenhe">
                                <option value="未审核">未审核</option>
                                <option value="已审核">已审核</option>
                            </select>
                        </div>
                    </div>


                    <div class="layui-form-item layui-layout-admin">
                        <div class="layui-input-block">
                            <div class="layui-footer" style="left: 0;">
                                <button class="layui-btn" lay-submit lay-filter="formDemo"><i
                                        class="layui-icon layui-icon-add-circle"></i>添加
                                </button>
                                <button class="layui-btn layui-btn-primary" type="reset">重置</button>
                            </div>
                        </div>
                    </div>

                </form>
            </div>
            <div class="layui-col-md3 formbox">

            </div>
        </div>
    </div>
</div>
</body>
<script type="text/javascript">

        //进度条
        var element = layui.element;
        //上传
        var upload = layui.upload
        //表单
        var form = layui.form
        //图片
        var fileName = null
        //apk文件
        var apkFile = null
        //得到用户名id
        var adminId = layui.data("local_store").token;
        // var adminId = localStorage.getItem("admin_id");
        form.render();
        form.on('submit(formDemo)', function () {

            if (fileName == null) {
                layer.msg('请先选择图像', {
                    icon: 2,
                    time:800
                })
                return false;
            }

            if(apkFile == null)
            {
                layer.msg('请先选择上传的apk文件', {
                    icon: 7
                })
                return false;
            }
            $.ajax({
                type: 'post',
                url: '/admin/apk/addApk',
                data: {
                    apkName: $('#apkName').val(),
                    apkPackage: $('#apkPackage').val(),
                    apkPictureUrl:fileName,
                    apkIsItPublic: $('#apkIsItPublic').val(),
                    apkShenhe: $('#apkShenhe').val(),
                    apkPackage:package,
                    apkVersion:versionCode,
                    apkMd5:MD5,
                    apkUrl:filePath
                },
                success: function (res) {
                    console.log(res)
                    if (res.code == 200) {
                        layer.msg('新增成功', {
                            icon: 1
                        })
                        setTimeout(function () {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.location.reload();
                            parent.layer.close(index);
                        }, 1000)
                        return
                    }
                    if(res.code==500) {
                        layer.msg(res.data, {
                            icon: 2
                        })
                    }
                },
                error: function (res) {
                    layer.msg('服务器异常或网络原因', {
                        icon: 2
                    })
                }
            })
            return false;
        })


        //图片上传
        upload.render({
            elem: '#apkPictureUrl',
            url: '/admin/fileinfo/upload/image',
            choose: function (obj) {
                fileName = null;
                //预览
                obj.preview(function (index, file, result) {
                    $('#apkPictureUrl').attr('src', result)
                })
            },
            done: function (res, index, upload) { //上传完成
                fileName = res.data //传回来的新的文件名
            }

        });

        //文件上传
        upload.render({
            elem: '#upFile',
            url: '/admin/fileinfo/upload/image',
            exts:'apk',
            size:1024*100,
            accept: 'file', //普通文件
            progress:function(value){//上传进度回调 value进度值
                element.progress('demo', value+'%')//设置页面进度条
            },
            choose: function (obj) {
                apkFile = null;
                //预览
                obj.preview(function (index, file, result) {
                })
            },
            done: function (res,index, upload) { //上传完成
                layer.msg('上传成功',{
                    icon:1,
                    time:800
                });
                apkFile=res.data.package;
                $("#apkPackage").val(apkFile);
                package=res.data.package;
                versionName=res.data.versionName;
                versionCode=res.data.versionCode;
                MD5=res.data.MD5;
                filePath=res.data.filePath;
            }
        });
</script>

</html>
